import axios from "axios";

/*
  const state = {
  clientList: [],
  clientCascader: [],
  clientCascaderLite: [],
  userCascader: [],
  userCascaderLite: [],
  roomCascader: [
    { value: "管制一室", label: "管制一室" },
    { value: "管制二室", label: "管制二室" },
    { value: "其他", label: "其他" },
  ],
  groupCascader: [
    {
      value: "管制一室",
      label: "管制一室",
      children: [
        { value: "B1", label: "B1" },
        { value: "B2", label: "B2" },
        { value: "B3", label: "B3" },
        { value: "其他", label: "其他" },
      ],
    },
    {
      value: "管制二室",
      label: "管制二室",
      children: [
        { value: "A1", label: "A1" },
        { value: "A2", label: "A2" },
        { value: "A3", label: "A3" },
        { value: "其他", label: "其他" },
      ],
    },
    {
      value: "其他",
      label: "其他",
      children: [{ value: "其他", label: "其他" }],
    },
  ],
  clientRankList: [
    "见习管制员",
    "部分扇区放单管制员",
    "放单管制员",
    "岗位教员",
    "责任教员",
    "监控席管制员",
    "带班主任",
    "其他",
  ],
  clientRankCascader: [
    {
      value: "all",
      label: "全部",
      children: [
        {
          value: "见习管制员",
          label: "见习管制员",
        },
        {
          value: "部分扇区放单管制员",
          label: "部分扇区放单管制员",
        },
        {
          value: "放单管制员",
          label: "放单管制员",
        },
        {
          value: "岗位教员",
          label: "岗位教员",
        },
        {
          value: "责任教员",
          label: "责任教员",
        },
        {
          value: "监控席管制员",
          label: "监控席管制员",
        },
        {
          value: "带班主任",
          label: "带班主任",
        },
        {
          value: "其他",
          label: "其他",
        },
      ],
    },
  ],
};
*/

const state = {
  clientList: [],
  clientCascader: [],
  clientCascaderLite: [],
  userCascader: [],
  userCascaderLite: [],
  clientRankCascader: [],
  clientRankList: [],
  groupCascader: [],
  roomCascader: [],
};

const mutations = {
  setClientList(state, payload) {
    state.clientList = payload;
    let clientCascaderLite = JSON.parse(JSON.stringify(state.roomCascader));
    let clientCascader = JSON.parse(JSON.stringify(state.groupCascader));
    let userCascaderLite = JSON.parse(JSON.stringify(state.roomCascader));
    let userCascader = JSON.parse(JSON.stringify(state.groupCascader));
    for (let i = 0; i < payload.length; i++) {
      for (let j = 0; j < clientCascaderLite.length; j++) {
        let label = payload[i].name + (+payload[i].interest > 0 ? " ★" : "");
        let value = payload[i].name;
        //clientCascaderLite
        if (payload[i].clientOffice == clientCascaderLite[j].value) {
          if (!clientCascaderLite[j]["children"]) {
            clientCascaderLite[j]["children"] = [];
          }
          clientCascaderLite[j].children.push({ label, value });
          //userCascaderLite
          if (+payload[i].level > 0 && +payload[i].level < 9) {
            if (!userCascaderLite[j]["children"]) {
              userCascaderLite[j]["children"] = [];
            }
            userCascaderLite[j].children.push({ label, value });
          }
          for (let k = 0; k < clientCascader[j].children.length; k++) {
            //clientCascader
            if (payload[i].clientGroup == clientCascader[j].children[k].value) {
              if (!clientCascader[j].children[k]["children"]) {
                clientCascader[j].children[k]["children"] = [];
              }
              clientCascader[j].children[k].children.push({ label, value });
              //userCascader
              if (+payload[i].level > 0 && +payload[i].level < 9) {
                if (!userCascader[j].children[k]["children"]) {
                  userCascader[j].children[k]["children"] = [];
                }
                userCascader[j].children[k].children.push({ label, value });
              }
            }
          }
        }
      }
    }
    state.clientCascaderLite = clientCascaderLite;
    state.clientCascader = clientCascader;
    state.userCascaderLite = userCascaderLite;
    state.userCascader = userCascader;
    // console.log(clientCascaderLite);
    // console.log(clientCascader);
    // console.log(userCascaderLite);
    // console.log(userCascader);
    // console.log(payload);
  },
  setState(state, payload) {
    let clientRankCascader = [
      {
        value: "all",
        label: "全部",
        children: payload.clientRank,
      },
    ];
    state.clientRankCascader = clientRankCascader;
    let clientRankList = [];
    for (let i = 0; i < payload.clientRank.length; i++) {
      clientRankList.push(payload.clientRank[i].value);
    }
    state.clientRankList = clientRankList;
    state.groupCascader = payload.clientOffice;
    let roomCascader = [];
    for (let i = 0; i < payload.clientOffice.length; i++) {
      roomCascader.push({
        value: payload.clientOffice[i].value,
        label: payload.clientOffice[i].label,
      });
    }
    state.roomCascader = roomCascader;
    console.log(state);
  },
};

const actions = {
  loadClientList({ commit, rootGetters }) {
    let params = ["clientOffice", "clientRank"];
    let user = rootGetters.userbase;
    axios
      .post("./api/load_params/", { user, params })
      .then((res) => {
        if (res.data.status == 0) {
          commit("setState", res.data.params);
          //----------------------------------------------------------------
          axios
            .post("api/query_security_data/", {
              user: rootGetters.userbase,
            })
            .then((response) => {
              if (response.data.status === 0) {
                commit("setClientList", response.data.content);
              } else {
                console.log(response.data);
              }
            })
            .catch((exception) => {
              console.log(exception);
            });
          //----------------------------------------------------------------
        } else {
          window.console.log(res);
        }
      })
      .catch((err) => {
        window.console.log(err);
      });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
